using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;
using OhioState.Math;

namespace ShapeEditor
{
    public partial class AxesPlaneControl : UserControl
    {
        private AxesPlaneProxy axesPlaneProxy;
        private AssetManager assetMan;
               
        private delegate void AxesPlaneHandler(AxesPlaneProxy slp);

        public AxesPlaneControl()
        {
            InitializeComponent();
            assetMan = AssetManager.Instance;
            assetManagerDropDownControl1.Init(typeof(IShape));
            /*assetManagerDropDownControl2.Init(typeof(IPlane));*/

            assetManagerDropDownControl1.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl1_SelectedEvent);
            /*assetManagerDropDownControl2.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl2_SelectedEvent);*/
        }

        #region Public Properties
        //
        // Public properties
        //
        public AxesPlaneProxy AxesPlane
        {
            get { return axesPlaneProxy; }
        }

        //
        // Set the focus of this control to a new axesPlane.  Be careful of thread issues
        private void SetAxesPlane_thisThread(AxesPlaneProxy AxesPlane)
        {
            axesPlaneProxy = null;
            //
            // Toggle Enabled based on whether the AxesPlane is null or not
            //
            if (AxesPlane == null)
            {
                this.Enabled = false;
            }
            else
            {
                this.Enabled = true;

                //
                // Populate control with SlicePlane values
                //
                IShape ishape = AxesPlane.IShape;
                assetManagerDropDownControl1.SelectItem((IAssetManaged)ishape);

                /*Plane plane = SlicePlane.Plane;
                assetManagerDropDownControl2.SelectItem(plane);           
                */
            }
            axesPlaneProxy = AxesPlane;
        }
        //
        // Force the event to get called on the proper thread.  The DataGridView complains if you try
        //  to use it on the wrong thread.
        //
        public void SetAxesPlane(AxesPlaneProxy AxesPlane)
        {
            AxesPlaneHandler newEvent = new AxesPlaneHandler(SetAxesPlane_thisThread);
            object[] args = new object[1];
            args[0] = AxesPlane;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(newEvent, args);
                else
                    newEvent(AxesPlane);
            }
            catch { }
        }

        #endregion

        private void assetManagerDropDownControl1_SelectedEvent(IAssetManaged obj)
        {
            if (axesPlaneProxy != null)
            {
                axesPlaneProxy.IShape = (IShape)obj;
            }
        }

       /* private void assetManagerDropDownControl2_SelectedEvent(IAssetManaged obj)
        {
            if (slicePlaneProxy != null)
            {
                slicePlaneProxy.Plane = (Plane)obj;
            }
        }*/


        public IShape SelectShapeDefault()
        {
            List<IShape> iShapes = new List<IShape>();
            // Hack for now only SharedBox
            List<IAssetManaged> list2 = assetMan.FindAll(typeof(SharedBox));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is IShape)
                    iShapes.Add((IShape)obj);
            }
            if (iShapes.Count > 0)
            {
                assetManagerDropDownControl1.SelectItem((IAssetManaged)iShapes[0]);
                return iShapes[0];
            }

            return null;
        }

        public Vector3 SelectPointDefault()
        {
            numeric_xPt.Value = (decimal)0.5;
            numeric_yPt.Value = (decimal)0.5f;
            numeric_zPt.Value = (decimal)0.5f;
            Vector3 pt = new Vector3(0.5f, 0.5f, 0.5f);
            return pt;
        }
        
        /*public Plane SelectPlaneDefault()
        {
            List<Plane> planes = new List<Plane>();
            List<IAssetManaged> list2 = assetMan.GetItem(typeof(Plane));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is Plane)
                    planes.Add((Plane)obj);
            }
            if (planes.Count > 0)
            {
                assetManagerDropDownControl2.SelectItem((IAssetManaged)planes[0]);
                return planes[0];
            }

            return null;
        }*/

        public delegate void AxesPlaneVisibility(bool isVisible);

        public void Visibility(bool isVisible)
        {
            AxesPlaneVisibility axesPlaneVisibility = new AxesPlaneVisibility(IsVisible_ThisThread);

            object[] args = new object[1];
            args[0] = isVisible;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(axesPlaneVisibility, args);
                else
                    axesPlaneVisibility(isVisible);
            }
            catch { }

        }

        public void IsVisible_ThisThread(bool isVisible)
        {
            this.Visible = isVisible;
        }

        private void Numeric_xPt_ValueChanged(object sender, EventArgs e)
        {
            if (axesPlaneProxy != null)
            {
                axesPlaneProxy.BeginManipulation();
                float xNew = Decimal.ToSingle(numeric_xPt.Value);
                axesPlaneProxy.Point = new Vector3(xNew, (float)numeric_yPt.Value, (float)numeric_zPt.Value);
                axesPlaneProxy.EndManipulation();
            }
        }

        private void Numeric_yPt_ValueChanged(object sender, EventArgs e)
        {
            if (axesPlaneProxy != null)
            {
                axesPlaneProxy.BeginManipulation();
                float yNew = Decimal.ToSingle(numeric_yPt.Value);
                axesPlaneProxy.Point = new Vector3((float)numeric_xPt.Value, yNew, (float)numeric_zPt.Value);
                axesPlaneProxy.EndManipulation();
            }

        }

        private void Numeric_zPt_ValueChanged(object sender, EventArgs e)
        {
            if (axesPlaneProxy != null)
            {
                axesPlaneProxy.BeginManipulation();
                float zNew = Decimal.ToSingle(numeric_zPt.Value);
                axesPlaneProxy.Point = new Vector3((float)numeric_xPt.Value, (float)numeric_yPt.Value, zNew);
                axesPlaneProxy.EndManipulation();
            }
        }
    }
}
